package com.selune.online.ordering.system.project.demo.service;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.demo.entity.Demo;
import com.selune.online.ordering.system.project.demo.entity.DemoSearch;
import com.selune.online.ordering.system.project.demo.mapper.DemoMapper;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author xiaoyp
 * @date 2020/2/27
 */

@Service
public class DemoServiceImpl implements DemoService {
	
	@Resource
	private DemoMapper demoMapper;
	
	@Override
	public Response<Boolean> save(Demo demo) {
		int result = demoMapper.insert(demo);
		if (result == 1) {
			return Response.success("添加用户成功", Boolean.TRUE);
		} else {
			return Response.error("添加用户失败!");
		}
	}
	
	@Override
	public Response<List<Demo>> list() {
		List<Demo> demos = demoMapper.selectList(null);
		return Response.success(demos);
	}
	
	@Override
	public Response<Boolean> update(Long id, Demo demo) {
		
		return Response.success(demoMapper.update(new Demo().setName(demo.getName()),
			Wrappers.<Demo>lambdaUpdate().set(Demo::getAge, demo.getAge())
				.set(Demo::getEmail, demo.getEmail())
				.eq(Demo::getId, id)
		) == 1 ? Boolean.TRUE : Boolean.FALSE);
	}
	
	@Override
	public Response<Demo> select(Long id) {
		Demo demo = demoMapper.selectOne(Wrappers.<Demo>lambdaQuery().eq(Demo::getId, id));
		if (ObjectUtils.isEmpty(demo)) {
			return Response.error("not exist");
		}
		return Response.success(demo);
	}
	
	@Override
	public Response<Page<Demo>> page(Integer current, Integer size) {
		Page<Demo> page = new Page<>(current, size);
		Page<Demo> testPage = demoMapper.selectPage(page, Wrappers.<Demo>lambdaQuery().orderByAsc(Demo::getAge));
		return Response.success(testPage);
	}
	
	@Override
	public Response<Page<Demo>> pageByCondition(Integer current, Integer size, DemoSearch demoSearch) {
		int newCurrent = null == current ? 1 : current;
		int pageSize = null == size ? 10 : size;
		Page<Demo> page = new Page<>(newCurrent, pageSize);
		Page<Demo> testPage = demoMapper.selectPage(page, Wrappers.<Demo>lambdaQuery()
			.eq(ObjectUtils.isNotEmpty(demoSearch.getAge()), Demo::getAge, demoSearch.getAge())
			.eq(StringUtils.isNotBlank(demoSearch.getEmail()), Demo::getEmail, demoSearch.getEmail())
			.like(StringUtils.isNotBlank(demoSearch.getName()), Demo::getName, demoSearch.getName())
			.orderByAsc(Demo::getAge));
		
		return Response.success(testPage);
	}
	
	@Override
	public Response<Boolean> deleteById(Long id) {
		return Response.success(demoMapper.deleteById(id) == 1 ? Boolean.TRUE : Boolean.FALSE);
	}
	
	@Override
//	@Transactional(rollbackFor = Exception.class)
	public Response<Boolean> deleteByIds(List<Long> ids) {
		return Response.success(demoMapper.deleteBatchIds(ids) == ids.size() ? Boolean.TRUE : Boolean.FALSE);
	}
	
	
}
